/**
 * 
 */
package com.applink.sell_proof.database;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;

import com.applink.sell_proof.database.table.AnalyticDBHelper;
import com.applink.sell_proof.database.table.ClaimDBHelper;
import com.applink.sell_proof.database.table.CountryDBHelper;
import com.applink.sell_proof.database.table.DealDBHelper;
import com.applink.sell_proof.database.table.DistributorDBHelper;
import com.applink.sell_proof.database.table.ImageDBHelper;
import com.applink.sell_proof.database.table.PriceBookDBHelper;
import com.applink.sell_proof.database.table.ProductImageDBHelper;
import com.applink.sell_proof.database.table.ProductModelDBHelper;
import com.applink.sell_proof.database.table.ProjectDBHelper;
import com.applink.sell_proof.database.table.PromotionDBHelper;
import com.applink.sell_proof.database.table.QuizDBHelper;
import com.applink.sell_proof.database.table.RedeemDBHelper;
import com.applink.sell_proof.database.table.RewardClaimDBHelper;
import com.applink.sell_proof.database.table.RewardClaimDetailDBHelper;
import com.applink.sell_proof.database.table.RewardDBHelper;
import com.applink.sell_proof.database.table.RewardProjectDBHelper;
import com.applink.sell_proof.database.table.RewardProjectDetailDBHelper;
import com.applink.sell_proof.database.table.TipDBHelper;
import com.applink.sell_proof.database.table.UserSettingDBHelper;

/**
 * @author vviieett
 * 
 */
public class EMarketingContentProvider extends ContentProvider {

	/* database helper */
	EMarketingDatabaseHelper mDatabaseHelper;

	/* key for uri matches */
	private static final int DEAL_KEY = 1;
	private static final int DEAL_ROW_KEY = 2;

	private static final int PRODUCT_KEY = 3;
	private static final int PRODUCT_ROW_KEY = 4;

	private static final int DISTRIBUTOR_KEY = 5;
	private static final int DISTRIBUTOR_ROW_KEY = 6;

	private static final int COUNTRY_KEY = 7;
	private static final int COUNTRY_ROW_KEY = 8;

	private static final int TIP_KEY = 9;
	private static final int TIP_ROW_KEY = 10;

	private static final int QUIZ_KEY = 11;
	private static final int QUIZ_ROW_KEY = 12;

	private static final int REDEEM_KEY = 13;
	private static final int REDEEM_ROW_KEY = 14;

	private static final int PRICEBOOK_KEY = 15;
	private static final int PRICEBOOK_ROW_KEY = 16;

	private static final int ANALYTICDATA_KEY = 17;
	private static final int ANALYTICDATA_ROW_KEY = 18;

	private static final int PROJECT_KEY = 19;
	private static final int PROJECT_ROW_KEY = 20;

	private static final int PROMOTION_KEY = 21;
	private static final int PROMOTION_ROW_KEY = 22;

	private static final int CLAIM_KEY = 23;
	private static final int CLAIM_ROW_KEY = 24;

	private static final int IMAGE_KEY = 25;
	private static final int IMAGE_ROW_KEY = 26;

	private static final int MODEL_KEY = 27;
	private static final int MODEL_ROW_KEY = 28;

	private static final int REWARD_KEY = 29;
	private static final int REWARD_ROW_KEY = 30;

	private static final int REWARD_PROJECT_KEY = 31;
	private static final int REWARD_PROJECT_ROW_KEY = 32;

	private static final int REWARD_CLAIM_KEY = 33;
	private static final int REWARD_CLAIM_ROW_KEY = 34;

	private static final int REWARD_PROJECT_DETAIL_KEY = 35;
	private static final int REWARD_PROJECT_DETAIL_ROW_KEY = 36;

	private static final int REWARD_CLAIM_DETAIL_KEY = 37;
	private static final int REWARD_CLAIM_DETAIL_ROW_KEY = 38;

	private static final int PRODUCT_IMAGE_KEY = 39;
	private static final int PRODUCT_IMAGE_ROW_KEY = 40;

	private static final int USER_SETTING_KEY = 41;
	private static final int USER_SETTING_ROW_KEY = 42;

	/* authority */
	private static final String AUTHORITY = "com.aplink.sell_proof.provider";

	/* Uri Matches */
	private static UriMatcher sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);

	/* path */
	private static final String DEAL_PATH = "deal";
	private static final String PRODUCT_PATH = "product";
	private static final String DISTRIBUTOR_PATH = "distributor";
	private static final String COUNTRY_PATH = "country";
	private static final String TIP_PATH = "tip";
	private static final String QUIZ_PATH = "quiz";
	private static final String REDEEM_PATH = "redeem";
	private static final String PRICEBOOK_PATH = "pricebook";
	private static final String ANALYTICDATA_PATH = "analyticdata";
	private static final String PROJECT_PATH = "project";
	private static final String PROMOTION_PATH = "promotion";
	private static final String PRODUCT_IMAGE_PATH = "product_image";
	private static final String CLAIM_PATH = "claim";
	private static final String IMAGE_PATH = "image";
	private static final String MODEL_PATH = "model";
	private static final String REWARD_PATH = "reward";
	private static final String REWARD_PROJECT_PATH = "reward_project";
	private static final String REWARD_CLAIM_PATH = "reward_claim";
	private static final String REWARD_PROJECT_DETAIL_PATH = "reward_project_detail";
	private static final String REWARD_CLAIM_DETAIL_PATH = "reward_claim_detail";
	private static final String USER_SETTING_PATH = "user_setting";

	/* content uri */
	public static final Uri USER_SETTING_CONTENT_URI = Uri.parse("content://"
			+ AUTHORITY + "/" + USER_SETTING_PATH);

	public static final Uri DEAL_CONTENT_URI = Uri.parse("content://"
			+ AUTHORITY + "/" + DEAL_PATH);

	public static final Uri PRODUCT_CONTENT_URI = Uri.parse("content://"
			+ AUTHORITY + "/" + PRODUCT_PATH);

	public static final Uri DISTRIBUTOR_CONTENT_URI = Uri.parse("content://"
			+ AUTHORITY + "/" + DISTRIBUTOR_PATH);

	public static final Uri COUNTRY_CONTENT_URI = Uri.parse("content://"
			+ AUTHORITY + "/" + COUNTRY_PATH);

	public static final Uri TIP_CONTENT_URI = Uri.parse("content://"
			+ AUTHORITY + "/" + TIP_PATH);

	public static final Uri QUIZ_CONTENT_URI = Uri.parse("content://"
			+ AUTHORITY + "/" + QUIZ_PATH);

	public static final Uri REDEEM_CONTENT_URI = Uri.parse("content://"
			+ AUTHORITY + "/" + REDEEM_PATH);

	public static final Uri PRICEBOOK_CONTENT_URI = Uri.parse("content://"
			+ AUTHORITY + "/" + PRICEBOOK_PATH);

	public static final Uri ANALYTICDATA_CONTENT_URI = Uri.parse("content://"
			+ AUTHORITY + "/" + ANALYTICDATA_PATH);

	public static final Uri PROJECT_CONTENT_URI = Uri.parse("content://"
			+ AUTHORITY + "/" + PROJECT_PATH);

	public static final Uri PROMOTION_CONTENT_URI = Uri.parse("content://"
			+ AUTHORITY + "/" + PROMOTION_PATH);

	public static final Uri PRODUCT_IMAGE_CONTENT_URI = Uri.parse("content://"
			+ AUTHORITY + "/" + PRODUCT_IMAGE_PATH);

	public static final Uri CLAIM_CONTENT_URI = Uri.parse("content://"
			+ AUTHORITY + "/" + CLAIM_PATH);

	public static final Uri IMAGE_CONTENT_URI = Uri.parse("content://"
			+ AUTHORITY + "/" + IMAGE_PATH);

	public static final Uri MODEL_CONTENT_URI = Uri.parse("content://"
			+ AUTHORITY + "/" + MODEL_PATH);

	public static final Uri REWARD_CONTENT_URI = Uri.parse("content://"
			+ AUTHORITY + "/" + REWARD_PATH);

	public static final Uri REWARD_PROJECT_CONTENT_URI = Uri.parse("content://"
			+ AUTHORITY + "/" + REWARD_PROJECT_PATH);

	public static final Uri REWARD_CLAIM_CONTENT_URI = Uri.parse("content://"
			+ AUTHORITY + "/" + REWARD_CLAIM_PATH);

	public static final Uri REWARD_PROJECT_DETAIL_CONTENT_URI = Uri
			.parse("content://" + AUTHORITY + "/" + REWARD_PROJECT_DETAIL_PATH);

	public static final Uri REWARD_CLAIM_DETAIL_CONTENT_URI = Uri
			.parse("content://" + AUTHORITY + "/" + REWARD_CLAIM_DETAIL_PATH);

	static {
		sUriMatcher.addURI(AUTHORITY, DEAL_PATH, DEAL_KEY);
		sUriMatcher.addURI(AUTHORITY, DEAL_PATH + "/#", DEAL_ROW_KEY);

		sUriMatcher.addURI(AUTHORITY, PRODUCT_PATH, PRODUCT_KEY);
		sUriMatcher.addURI(AUTHORITY, PRODUCT_PATH + "/#", PRODUCT_ROW_KEY);

		sUriMatcher.addURI(AUTHORITY, DISTRIBUTOR_PATH, DISTRIBUTOR_KEY);
		sUriMatcher.addURI(AUTHORITY, DISTRIBUTOR_PATH + "/#",
				DISTRIBUTOR_ROW_KEY);

		sUriMatcher.addURI(AUTHORITY, COUNTRY_PATH, COUNTRY_KEY);
		sUriMatcher.addURI(AUTHORITY, COUNTRY_PATH + "/#", COUNTRY_ROW_KEY);

		sUriMatcher.addURI(AUTHORITY, TIP_PATH, TIP_KEY);
		sUriMatcher.addURI(AUTHORITY, TIP_PATH + "/#", TIP_ROW_KEY);

		sUriMatcher.addURI(AUTHORITY, QUIZ_PATH, QUIZ_KEY);
		sUriMatcher.addURI(AUTHORITY, QUIZ_PATH + "/#", QUIZ_ROW_KEY);

		sUriMatcher.addURI(AUTHORITY, REDEEM_PATH, REDEEM_KEY);
		sUriMatcher.addURI(AUTHORITY, REDEEM_PATH + "/#", REDEEM_ROW_KEY);

		sUriMatcher.addURI(AUTHORITY, PRICEBOOK_PATH, PRICEBOOK_KEY);
		sUriMatcher.addURI(AUTHORITY, PRICEBOOK_PATH + "/#", PRICEBOOK_ROW_KEY);

		sUriMatcher.addURI(AUTHORITY, ANALYTICDATA_PATH, ANALYTICDATA_KEY);
		sUriMatcher.addURI(AUTHORITY, ANALYTICDATA_PATH + "/#",
				ANALYTICDATA_ROW_KEY);

		sUriMatcher.addURI(AUTHORITY, PROJECT_PATH, PROJECT_KEY);
		sUriMatcher.addURI(AUTHORITY, PROJECT_PATH + "/#", PROJECT_ROW_KEY);

		sUriMatcher.addURI(AUTHORITY, PROMOTION_PATH, PROMOTION_KEY);
		sUriMatcher.addURI(AUTHORITY, PROMOTION_PATH + "/#", PROMOTION_ROW_KEY);

		sUriMatcher.addURI(AUTHORITY, PRODUCT_IMAGE_PATH, PRODUCT_IMAGE_KEY);
		sUriMatcher.addURI(AUTHORITY, PRODUCT_IMAGE_PATH + "/#",
				PRODUCT_IMAGE_ROW_KEY);

		sUriMatcher.addURI(AUTHORITY, USER_SETTING_PATH, USER_SETTING_KEY);
		sUriMatcher.addURI(AUTHORITY, USER_SETTING_PATH + "/#",
				USER_SETTING_ROW_KEY);

		sUriMatcher.addURI(AUTHORITY, CLAIM_PATH, CLAIM_KEY);
		sUriMatcher.addURI(AUTHORITY, CLAIM_PATH + "/#", CLAIM_ROW_KEY);

		sUriMatcher.addURI(AUTHORITY, IMAGE_PATH, IMAGE_KEY);
		sUriMatcher.addURI(AUTHORITY, IMAGE_PATH + "/#", IMAGE_ROW_KEY);

		sUriMatcher.addURI(AUTHORITY, MODEL_PATH, MODEL_KEY);
		sUriMatcher.addURI(AUTHORITY, MODEL_PATH + "/#", MODEL_ROW_KEY);

		sUriMatcher.addURI(AUTHORITY, REWARD_PATH, REWARD_KEY);
		sUriMatcher.addURI(AUTHORITY, REWARD_PATH + "/#", REWARD_ROW_KEY);

		sUriMatcher.addURI(AUTHORITY, REWARD_PROJECT_PATH, REWARD_PROJECT_KEY);
		sUriMatcher.addURI(AUTHORITY, REWARD_PROJECT_PATH + "/#",
				REWARD_PROJECT_ROW_KEY);

		sUriMatcher.addURI(AUTHORITY, REWARD_CLAIM_PATH, REWARD_CLAIM_KEY);
		sUriMatcher.addURI(AUTHORITY, REWARD_CLAIM_PATH + "/#",
				REWARD_CLAIM_ROW_KEY);

		sUriMatcher.addURI(AUTHORITY, REWARD_PROJECT_DETAIL_PATH,
				REWARD_PROJECT_DETAIL_KEY);
		sUriMatcher.addURI(AUTHORITY, REWARD_PROJECT_DETAIL_PATH + "/#",
				REWARD_PROJECT_DETAIL_ROW_KEY);

		sUriMatcher.addURI(AUTHORITY, REWARD_CLAIM_DETAIL_PATH,
				REWARD_CLAIM_DETAIL_KEY);
		sUriMatcher.addURI(AUTHORITY, REWARD_CLAIM_DETAIL_PATH + "/#",
				REWARD_CLAIM_DETAIL_ROW_KEY);

	}

	@Override
	public int delete(Uri uri, String selection, String[] selectionArgs) {
		int row_deleted = 0;
		SQLiteDatabase db = null;
		db = mDatabaseHelper.getWritableDatabase();
		int uriKey = sUriMatcher.match(uri);
		switch (uriKey) {

		case DEAL_ROW_KEY:
			row_deleted = db.delete(DealDBHelper.TABLE_NAME, DealDBHelper.ID
					+ " = " + uri.getLastPathSegment() + " and " + selection,
					selectionArgs);
			getContext().getContentResolver().notifyChange(uri, null);
			break;
		case DEAL_KEY:
			row_deleted = db.delete(DealDBHelper.TABLE_NAME, selection,
					selectionArgs);
			getContext().getContentResolver().notifyChange(uri, null);
			break;
		case REDEEM_ROW_KEY:
			row_deleted = db.delete(RedeemDBHelper.TABLE_NAME,
					RedeemDBHelper.REDEEM_ID + " = " + uri.getLastPathSegment()
							+ " and " + selection, selectionArgs);
			getContext().getContentResolver().notifyChange(uri, null);
			break;
		case REDEEM_KEY:
			row_deleted = db.delete(RedeemDBHelper.TABLE_NAME, selection,
					selectionArgs);
			getContext().getContentResolver().notifyChange(uri, null);
			break;
		case PRICEBOOK_ROW_KEY:
			row_deleted = db.delete(PriceBookDBHelper.TABLE_NAME,
					PriceBookDBHelper.PB_ID + " = " + uri.getLastPathSegment()
							+ " and " + selection, selectionArgs);
			getContext().getContentResolver().notifyChange(uri, null);
			break;
		case PRICEBOOK_KEY:
			row_deleted = db.delete(PriceBookDBHelper.TABLE_NAME, selection,
					selectionArgs);
			getContext().getContentResolver().notifyChange(uri, null);
			break;
		case ANALYTICDATA_ROW_KEY:
			row_deleted = db.delete(AnalyticDBHelper.TABLE_NAME,
					AnalyticDBHelper.ID + " = " + uri.getLastPathSegment()
							+ " and " + selection, selectionArgs);
			getContext().getContentResolver().notifyChange(uri, null);
			break;
		case ANALYTICDATA_KEY:
			row_deleted = db.delete(AnalyticDBHelper.TABLE_NAME, selection,
					selectionArgs);
			getContext().getContentResolver().notifyChange(uri, null);
			break;
		case PROJECT_ROW_KEY:
			row_deleted = db.delete(ProjectDBHelper.TABLE_NAME,
					ProjectDBHelper.ID + " = " + uri.getLastPathSegment()
							+ " and " + selection, selectionArgs);
			getContext().getContentResolver().notifyChange(uri, null);
			break;
		case PROJECT_KEY:
			row_deleted = db.delete(ProjectDBHelper.TABLE_NAME, selection,
					selectionArgs);
			getContext().getContentResolver().notifyChange(uri, null);
			break;
		case PROMOTION_ROW_KEY:
			row_deleted = db.delete(PromotionDBHelper.TABLE_NAME,
					PromotionDBHelper.PM_ID + " = " + uri.getLastPathSegment()
							+ " and " + selection, selectionArgs);
			getContext().getContentResolver().notifyChange(uri, null);
			break;
		case PROMOTION_KEY:
			row_deleted = db.delete(PromotionDBHelper.TABLE_NAME, selection,
					selectionArgs);
			getContext().getContentResolver().notifyChange(uri, null);
			break;

		case PRODUCT_IMAGE_ROW_KEY:
			row_deleted = db.delete(
					ProductImageDBHelper.TABLE_NAME,
					ProductImageDBHelper.PDIM_ID + " = "
							+ uri.getLastPathSegment() + " and " + selection,
					selectionArgs);
			getContext().getContentResolver().notifyChange(uri, null);
			break;
		case PRODUCT_IMAGE_KEY:
			row_deleted = db.delete(ProductImageDBHelper.TABLE_NAME, selection,
					selectionArgs);
			getContext().getContentResolver().notifyChange(uri, null);
			break;

		case USER_SETTING_ROW_KEY:
			row_deleted = db.delete(
					UserSettingDBHelper.TABLE_NAME,
					UserSettingDBHelper.USER_ID + " = "
							+ uri.getLastPathSegment() + " and " + selection,
					selectionArgs);
			getContext().getContentResolver().notifyChange(uri, null);
			break;
		case USER_SETTING_KEY:
			row_deleted = db.delete(UserSettingDBHelper.TABLE_NAME, selection,
					selectionArgs);
			getContext().getContentResolver().notifyChange(uri, null);
			break;

		case CLAIM_ROW_KEY:
			row_deleted = db.delete(ClaimDBHelper.TABLE_NAME, ClaimDBHelper.ID
					+ " = " + uri.getLastPathSegment() + " and " + selection,
					selectionArgs);
			getContext().getContentResolver().notifyChange(uri, null);
			break;
		case CLAIM_KEY:
			row_deleted = db.delete(ClaimDBHelper.TABLE_NAME, selection,
					selectionArgs);
			getContext().getContentResolver().notifyChange(uri, null);
			break;
		case IMAGE_ROW_KEY:
			row_deleted = db.delete(ImageDBHelper.TABLE_NAME, ImageDBHelper.ID
					+ " = " + uri.getLastPathSegment() + " and " + selection,
					selectionArgs);
			getContext().getContentResolver().notifyChange(uri, null);
			break;
		case IMAGE_KEY:
			row_deleted = db.delete(ImageDBHelper.TABLE_NAME, selection,
					selectionArgs);
			getContext().getContentResolver().notifyChange(uri, null);
			break;
		case REWARD_PROJECT_ROW_KEY:
			row_deleted = db.delete(
					RewardProjectDBHelper.TABLE_NAME,
					RewardProjectDBHelper.RW_PJ_OBJECT_ID + " = "
							+ uri.getLastPathSegment() + " and " + selection,
					selectionArgs);
			getContext().getContentResolver().notifyChange(uri, null);
			break;
		case REWARD_PROJECT_KEY:
			row_deleted = db.delete(RewardProjectDBHelper.TABLE_NAME,
					selection, selectionArgs);
			getContext().getContentResolver().notifyChange(uri, null);
			break;
		case REWARD_CLAIM_ROW_KEY:
			row_deleted = db.delete(
					RewardClaimDBHelper.TABLE_NAME,
					RewardClaimDBHelper.RW_ID + " = "
							+ uri.getLastPathSegment() + " and " + selection,
					selectionArgs);
			getContext().getContentResolver().notifyChange(uri, null);
			break;
		case REWARD_CLAIM_KEY:
			row_deleted = db.delete(RewardClaimDBHelper.TABLE_NAME, selection,
					selectionArgs);
			getContext().getContentResolver().notifyChange(uri, null);
			break;
		case REWARD_PROJECT_DETAIL_ROW_KEY:
			row_deleted = db.delete(
					RewardProjectDetailDBHelper.TABLE_NAME,
					RewardProjectDetailDBHelper.RW_PG_OBJECT_ID + " = "
							+ uri.getLastPathSegment() + " and " + selection,
					selectionArgs);
			getContext().getContentResolver().notifyChange(uri, null);
			break;
		case REWARD_PROJECT_DETAIL_KEY:
			row_deleted = db.delete(RewardProjectDetailDBHelper.TABLE_NAME,
					selection, selectionArgs);
			getContext().getContentResolver().notifyChange(uri, null);
			break;
		case REWARD_CLAIM_DETAIL_ROW_KEY:
			row_deleted = db.delete(
					RewardClaimDetailDBHelper.TABLE_NAME,
					RewardClaimDetailDBHelper.RW_CL_DETAIL_ID + " = "
							+ uri.getLastPathSegment() + " and " + selection,
					selectionArgs);
			getContext().getContentResolver().notifyChange(uri, null);
			break;
		case REWARD_CLAIM_DETAIL_KEY:
			row_deleted = db.delete(RewardClaimDetailDBHelper.TABLE_NAME,
					selection, selectionArgs);
			getContext().getContentResolver().notifyChange(uri, null);
			break;
		default:
			throw new IllegalArgumentException("Unknown URI: " + uri);
		}
		return row_deleted;
	}

	@Override
	public String getType(Uri uri) {
		return null;
	}

	@Override
	public boolean onCreate() {
		mDatabaseHelper = new EMarketingDatabaseHelper(getContext());
		return true;
	}

	@Override
	public Cursor query(Uri uri, String[] projection, String selection,
			String[] selectionArgs, String sortOrder) {
		Cursor cursor = null;
		SQLiteDatabase db = mDatabaseHelper.getReadableDatabase();
		SQLiteQueryBuilder querybuilder = new SQLiteQueryBuilder();
		int uriKey = sUriMatcher.match(uri);
		switch (uriKey) {

		case DEAL_ROW_KEY:
			querybuilder.appendWhere(DealDBHelper.ID + " = "
					+ uri.getLastPathSegment());
		case DEAL_KEY:
			querybuilder.setTables(DealDBHelper.TABLE_NAME);
			cursor = querybuilder.query(db, null, selection, selectionArgs,
					null, null, sortOrder);
			cursor.setNotificationUri(getContext().getContentResolver(), uri);
			break;

		case DISTRIBUTOR_ROW_KEY:
			querybuilder.appendWhere(DistributorDBHelper.DISTRIBUTOR_ID + " = "
					+ uri.getLastPathSegment());
		case DISTRIBUTOR_KEY:
			querybuilder.setTables(DistributorDBHelper.TABLE_NAME);
			cursor = querybuilder.query(db, null, selection, selectionArgs,
					null, null, sortOrder);
			cursor.setNotificationUri(getContext().getContentResolver(), uri);
			break;

		case COUNTRY_ROW_KEY:
			querybuilder.appendWhere(CountryDBHelper.COUNTRY_ID + " = "
					+ uri.getLastPathSegment());
		case COUNTRY_KEY:
			querybuilder.setTables(CountryDBHelper.TABLE_NAME);
			cursor = querybuilder.query(db, null, selection, selectionArgs,
					null, null, sortOrder);
			cursor.setNotificationUri(getContext().getContentResolver(), uri);
			break;

		case TIP_ROW_KEY:
			querybuilder.appendWhere(TipDBHelper.TIP_ID + " = "
					+ uri.getLastPathSegment());
		case TIP_KEY:
			querybuilder.setTables(TipDBHelper.TABLE_NAME);
			querybuilder.setDistinct(true);
			cursor = querybuilder.query(db, null, selection, selectionArgs,
					null, null, sortOrder);
			cursor.setNotificationUri(getContext().getContentResolver(), uri);
			break;

		case QUIZ_ROW_KEY:
			querybuilder.appendWhere(QuizDBHelper.QUIZ_ID + " = "
					+ uri.getLastPathSegment());
		case QUIZ_KEY:
			querybuilder.setTables(QuizDBHelper.TABLE_NAME);
			querybuilder.setDistinct(true);
			cursor = querybuilder.query(db, null, selection, selectionArgs,
					null, null, sortOrder);
			cursor.setNotificationUri(getContext().getContentResolver(), uri);
			break;

		case REDEEM_ROW_KEY:
			querybuilder.appendWhere(RedeemDBHelper.REDEEM_ID + " = "
					+ uri.getLastPathSegment());
		case REDEEM_KEY:
			querybuilder.setTables(RedeemDBHelper.TABLE_NAME);
			querybuilder.setDistinct(true);
			cursor = querybuilder.query(db, null, selection, selectionArgs,
					null, null, sortOrder);
			cursor.setNotificationUri(getContext().getContentResolver(), uri);
			break;
		case PRICEBOOK_ROW_KEY:
			querybuilder.appendWhere(PriceBookDBHelper.PB_ID + " = "
					+ uri.getLastPathSegment());
		case PRICEBOOK_KEY:
			querybuilder.setTables(PriceBookDBHelper.TABLE_NAME);
			querybuilder.setDistinct(true);
			cursor = querybuilder.query(db, null, selection, selectionArgs,
					null, null, sortOrder);
			cursor.setNotificationUri(getContext().getContentResolver(), uri);
			break;

		case REWARD_ROW_KEY:
			querybuilder.appendWhere(RewardDBHelper.RW_OBJECT + " = "
					+ uri.getLastPathSegment());
		case REWARD_KEY:
			querybuilder.setTables(RewardDBHelper.TABLE_NAME);
			querybuilder.setDistinct(true);
			cursor = querybuilder.query(db, null, selection, selectionArgs,
					null, null, sortOrder);
			cursor.setNotificationUri(getContext().getContentResolver(), uri);
			break;

		case ANALYTICDATA_ROW_KEY:
			querybuilder.appendWhere(AnalyticDBHelper.ID + " = "
					+ uri.getLastPathSegment());
		case ANALYTICDATA_KEY:
			querybuilder.setTables(AnalyticDBHelper.TABLE_NAME);
			querybuilder.setDistinct(true);
			cursor = querybuilder.query(db, null, selection, selectionArgs,
					null, null, sortOrder);
			cursor.setNotificationUri(getContext().getContentResolver(), uri);
			break;
		case PROJECT_ROW_KEY:
			querybuilder.appendWhere(ProjectDBHelper.ID + " = "
					+ uri.getLastPathSegment());
		case PROJECT_KEY:
			querybuilder.setTables(ProjectDBHelper.TABLE_NAME);
			querybuilder.setDistinct(true);
			cursor = querybuilder.query(db, null, selection, selectionArgs,
					null, null, sortOrder);
			cursor.setNotificationUri(getContext().getContentResolver(), uri);
			break;
		case PROMOTION_ROW_KEY:
			querybuilder.appendWhere(PromotionDBHelper.PM_ID + " = "
					+ uri.getLastPathSegment());
		case PROMOTION_KEY:
			querybuilder.setTables(PromotionDBHelper.TABLE_NAME);
			querybuilder.setDistinct(true);
			cursor = querybuilder.query(db, null, selection, selectionArgs,
					null, null, sortOrder);
			cursor.setNotificationUri(getContext().getContentResolver(), uri);
			break;

		case PRODUCT_IMAGE_ROW_KEY:
			querybuilder.appendWhere(ProductImageDBHelper.PDIM_ID + " = "
					+ uri.getLastPathSegment());
		case PRODUCT_IMAGE_KEY:
			querybuilder.setTables(ProductImageDBHelper.TABLE_NAME);
			querybuilder.setDistinct(true);
			cursor = querybuilder.query(db, null, selection, selectionArgs,
					null, null, sortOrder);
			cursor.setNotificationUri(getContext().getContentResolver(), uri);
			break;

		case USER_SETTING_ROW_KEY:
			querybuilder.appendWhere(UserSettingDBHelper.USER_ID + " = "
					+ uri.getLastPathSegment());
		case USER_SETTING_KEY:
			querybuilder.setTables(UserSettingDBHelper.TABLE_NAME);
			querybuilder.setDistinct(true);
			cursor = querybuilder.query(db, null, selection, selectionArgs,
					null, null, sortOrder);
			cursor.setNotificationUri(getContext().getContentResolver(), uri);
			break;

		case CLAIM_ROW_KEY:
			querybuilder.appendWhere(ClaimDBHelper.ID + " = "
					+ uri.getLastPathSegment());
		case CLAIM_KEY:
			querybuilder.setTables(ClaimDBHelper.TABLE_NAME);
			querybuilder.setDistinct(true);
			cursor = querybuilder.query(db, null, selection, selectionArgs,
					null, null, sortOrder);
			cursor.setNotificationUri(getContext().getContentResolver(), uri);
			break;
		case IMAGE_ROW_KEY:
			querybuilder.appendWhere(ImageDBHelper.ID + " = "
					+ uri.getLastPathSegment());
		case IMAGE_KEY:
			querybuilder.setTables(ImageDBHelper.TABLE_NAME);
			querybuilder.setDistinct(true);
			cursor = querybuilder.query(db, null, selection, selectionArgs,
					null, null, sortOrder);
			cursor.setNotificationUri(getContext().getContentResolver(), uri);
			break;
		case MODEL_ROW_KEY:
			querybuilder.appendWhere(ProductModelDBHelper.ID + " = "
					+ uri.getLastPathSegment());
		case MODEL_KEY:
			querybuilder.setTables(ProductModelDBHelper.TABLE_NAME);
			querybuilder.setDistinct(true);
			cursor = querybuilder.query(db, null, selection, selectionArgs,
					null, null, sortOrder);
			cursor.setNotificationUri(getContext().getContentResolver(), uri);
			break;

		case REWARD_PROJECT_ROW_KEY:
			querybuilder.appendWhere(RewardProjectDBHelper.RW_PJ_OBJECT_ID
					+ " = " + uri.getLastPathSegment());
		case REWARD_PROJECT_KEY:
			querybuilder.setTables(RewardProjectDBHelper.TABLE_NAME);
			querybuilder.setDistinct(true);
			cursor = querybuilder.query(db, null, selection, selectionArgs,
					null, null, sortOrder);
			cursor.setNotificationUri(getContext().getContentResolver(), uri);
			break;

		case REWARD_CLAIM_ROW_KEY:
			querybuilder.appendWhere(RewardClaimDBHelper.RW_ID + " = "
					+ uri.getLastPathSegment());
		case REWARD_CLAIM_KEY:
			querybuilder.setTables(RewardClaimDBHelper.TABLE_NAME);
			querybuilder.setDistinct(true);
			cursor = querybuilder.query(db, null, selection, selectionArgs,
					null, null, sortOrder);
			cursor.setNotificationUri(getContext().getContentResolver(), uri);
			break;

		case REWARD_PROJECT_DETAIL_ROW_KEY:
			querybuilder
					.appendWhere(RewardProjectDetailDBHelper.RW_PG_OBJECT_ID
							+ " = " + uri.getLastPathSegment());
		case REWARD_PROJECT_DETAIL_KEY:
			querybuilder.setTables(RewardProjectDetailDBHelper.TABLE_NAME);
			querybuilder.setDistinct(true);
			cursor = querybuilder.query(db, null, selection, selectionArgs,
					null, null, sortOrder);
			cursor.setNotificationUri(getContext().getContentResolver(), uri);
			break;

		case REWARD_CLAIM_DETAIL_ROW_KEY:
			querybuilder.appendWhere(RewardClaimDetailDBHelper.RW_CL_DETAIL_ID
					+ " = " + uri.getLastPathSegment());
		case REWARD_CLAIM_DETAIL_KEY:
			querybuilder.setTables(RewardClaimDetailDBHelper.TABLE_NAME);
			querybuilder.setDistinct(true);
			cursor = querybuilder.query(db, null, selection, selectionArgs,
					null, null, sortOrder);
			cursor.setNotificationUri(getContext().getContentResolver(), uri);
			break;
		default:
			throw new IllegalArgumentException("Unknown URI: " + uri);
		}
		return cursor;
	}

	@Override
	public Uri insert(Uri uri, ContentValues values) {
		Uri result_uri = null;
		SQLiteDatabase db = null;
		db = mDatabaseHelper.getWritableDatabase();
		int uriKey = sUriMatcher.match(uri);
		switch (uriKey) {

		case DEAL_KEY:
			long id = db.insertWithOnConflict(DealDBHelper.TABLE_NAME, null,
					values, SQLiteDatabase.CONFLICT_REPLACE);
			result_uri = Uri.parse(DEAL_CONTENT_URI + "/" + Long.toString(id));
			getContext().getContentResolver().notifyChange(uri, null);
			break;

		case DISTRIBUTOR_KEY:
			long id_distributor = db.insertWithOnConflict(
					DistributorDBHelper.TABLE_NAME, null, values,
					SQLiteDatabase.CONFLICT_REPLACE);
			result_uri = Uri.parse(DISTRIBUTOR_CONTENT_URI + "/"
					+ Long.toString(id_distributor));
			getContext().getContentResolver().notifyChange(uri, null);
			break;

		case COUNTRY_KEY:
			long id_country = db.insertWithOnConflict(
					CountryDBHelper.TABLE_NAME, null, values,
					SQLiteDatabase.CONFLICT_REPLACE);
			result_uri = Uri.parse(COUNTRY_CONTENT_URI + "/"
					+ Long.toString(id_country));
			getContext().getContentResolver().notifyChange(uri, null);
			break;

		case TIP_KEY:
			long id_tip = db.insertWithOnConflict(TipDBHelper.TABLE_NAME, null,
					values, SQLiteDatabase.CONFLICT_REPLACE);
			result_uri = Uri.parse(TIP_CONTENT_URI + "/"
					+ Long.toString(id_tip));
			getContext().getContentResolver().notifyChange(uri, null);
			break;

		case QUIZ_KEY:
			long id_quiz = db.insertWithOnConflict(QuizDBHelper.TABLE_NAME,
					null, values, SQLiteDatabase.CONFLICT_REPLACE);
			result_uri = Uri.parse(QUIZ_CONTENT_URI + "/"
					+ Long.toString(id_quiz));
			getContext().getContentResolver().notifyChange(uri, null);
			break;

		case REDEEM_KEY:
			long id_redeem = db.insertWithOnConflict(RedeemDBHelper.TABLE_NAME,
					null, values, SQLiteDatabase.CONFLICT_REPLACE);
			result_uri = Uri.parse(REDEEM_CONTENT_URI + "/"
					+ Long.toString(id_redeem));
			getContext().getContentResolver().notifyChange(uri, null);
			break;
		case PRICEBOOK_KEY:
			long id_pricebook = db.insertWithOnConflict(
					PriceBookDBHelper.TABLE_NAME, null, values,
					SQLiteDatabase.CONFLICT_REPLACE);
			result_uri = Uri.parse(PRICEBOOK_CONTENT_URI + "/"
					+ Long.toString(id_pricebook));
			getContext().getContentResolver().notifyChange(uri, null);
			break;
		case ANALYTICDATA_KEY:
			long id_analyticdata = db.insertWithOnConflict(
					AnalyticDBHelper.TABLE_NAME, null, values,
					SQLiteDatabase.CONFLICT_REPLACE);
			result_uri = Uri.parse(ANALYTICDATA_CONTENT_URI + "/"
					+ Long.toString(id_analyticdata));
			getContext().getContentResolver().notifyChange(uri, null);
			break;
		case PROJECT_KEY:
			long id_project = db.insertWithOnConflict(
					ProjectDBHelper.TABLE_NAME, null, values,
					SQLiteDatabase.CONFLICT_REPLACE);
			result_uri = Uri.parse(PROJECT_CONTENT_URI + "/"
					+ Long.toString(id_project));
			getContext().getContentResolver().notifyChange(uri, null);
			break;
		case PROMOTION_KEY:
			long id_promotionbook = db.insertWithOnConflict(
					PromotionDBHelper.TABLE_NAME, null, values,
					SQLiteDatabase.CONFLICT_REPLACE);
			result_uri = Uri.parse(PROMOTION_CONTENT_URI + "/"
					+ Long.toString(id_promotionbook));
			getContext().getContentResolver().notifyChange(uri, null);
			break;

		case PRODUCT_IMAGE_KEY:
			long id_productImage = db.insertWithOnConflict(
					ProductImageDBHelper.TABLE_NAME, null, values,
					SQLiteDatabase.CONFLICT_REPLACE);
			result_uri = Uri.parse(PRODUCT_IMAGE_PATH + "/"
					+ Long.toString(id_productImage));
			getContext().getContentResolver().notifyChange(uri, null);
			break;

		case USER_SETTING_KEY:
			long id_userSetting = db.insertWithOnConflict(
					UserSettingDBHelper.TABLE_NAME, null, values,
					SQLiteDatabase.CONFLICT_REPLACE);
			result_uri = Uri.parse(USER_SETTING_PATH + "/"
					+ Long.toString(id_userSetting));
			getContext().getContentResolver().notifyChange(uri, null);
			break;

		case REWARD_KEY:
			long id_reward = db.insertWithOnConflict(RewardDBHelper.TABLE_NAME,
					null, values, SQLiteDatabase.CONFLICT_REPLACE);
			result_uri = Uri.parse(REWARD_CONTENT_URI + "/"
					+ Long.toString(id_reward));
			getContext().getContentResolver().notifyChange(uri, null);
			break;
		case CLAIM_KEY:
			long id_claim = db.insertWithOnConflict(ClaimDBHelper.TABLE_NAME,
					null, values, SQLiteDatabase.CONFLICT_REPLACE);
			result_uri = Uri.parse(CLAIM_CONTENT_URI + "/"
					+ Long.toString(id_claim));
			getContext().getContentResolver().notifyChange(uri, null);
			break;
		case IMAGE_KEY:
			long id_image = db.insertWithOnConflict(ImageDBHelper.TABLE_NAME,
					null, values, SQLiteDatabase.CONFLICT_REPLACE);
			result_uri = Uri.parse(IMAGE_CONTENT_URI + "/"
					+ Long.toString(id_image));
			getContext().getContentResolver().notifyChange(uri, null);
			break;
		case MODEL_KEY:
			long id_model = db.insertWithOnConflict(
					ProductModelDBHelper.TABLE_NAME, null, values,
					SQLiteDatabase.CONFLICT_REPLACE);
			result_uri = Uri.parse(MODEL_CONTENT_URI + "/"
					+ Long.toString(id_model));
			getContext().getContentResolver().notifyChange(uri, null);
			break;
		case REWARD_PROJECT_KEY:
			long id_reward_project = db.insertWithOnConflict(
					RewardProjectDBHelper.TABLE_NAME, null, values,
					SQLiteDatabase.CONFLICT_REPLACE);
			result_uri = Uri.parse(REWARD_PROJECT_CONTENT_URI + "/"
					+ Long.toString(id_reward_project));
			getContext().getContentResolver().notifyChange(uri, null);
			break;
		case REWARD_CLAIM_KEY:
			long id_reward_claim = db.insertWithOnConflict(
					RewardClaimDBHelper.TABLE_NAME, null, values,
					SQLiteDatabase.CONFLICT_REPLACE);
			result_uri = Uri.parse(REWARD_CLAIM_CONTENT_URI + "/"
					+ Long.toString(id_reward_claim));
			getContext().getContentResolver().notifyChange(uri, null);
			break;
		case REWARD_PROJECT_DETAIL_KEY:
			long id_reward_project_detail = db.insertWithOnConflict(
					RewardProjectDetailDBHelper.TABLE_NAME, null, values,
					SQLiteDatabase.CONFLICT_REPLACE);
			result_uri = Uri.parse(REWARD_PROJECT_DETAIL_CONTENT_URI + "/"
					+ Long.toString(id_reward_project_detail));
			getContext().getContentResolver().notifyChange(uri, null);
			break;
		case REWARD_CLAIM_DETAIL_KEY:
			long id_reward_claim_detail = db.insertWithOnConflict(
					RewardClaimDetailDBHelper.TABLE_NAME, null, values,
					SQLiteDatabase.CONFLICT_REPLACE);
			result_uri = Uri.parse(REWARD_CLAIM_DETAIL_CONTENT_URI + "/"
					+ Long.toString(id_reward_claim_detail));
			getContext().getContentResolver().notifyChange(uri, null);
			break;
		default:
			throw new IllegalArgumentException("Unknown URI: " + uri);
		}
		return result_uri;
	}

	@Override
	public int update(Uri uri, ContentValues values, String selection,
			String[] selectionArgs) {
		int row_update = 0;
		SQLiteDatabase db = mDatabaseHelper.getWritableDatabase();
		int uriKey = sUriMatcher.match(uri);
		switch (uriKey) {

		case DEAL_ROW_KEY:
			row_update = db.update(DealDBHelper.TABLE_NAME, values,
					DealDBHelper.ID + "=?",
					new String[] { uri.getLastPathSegment() });
			getContext().getContentResolver().notifyChange(uri, null);
			break;
		case DEAL_KEY:
			row_update = db.update(DealDBHelper.TABLE_NAME, values, selection,
					selectionArgs);
			getContext().getContentResolver().notifyChange(uri, null);
			break;

		case TIP_ROW_KEY:
			row_update = db.update(TipDBHelper.TABLE_NAME, values,
					TipDBHelper.TIP_ID + "=?",
					new String[] { uri.getLastPathSegment() });
			getContext().getContentResolver().notifyChange(uri, null);
			break;
		case TIP_KEY:
			row_update = db.update(TipDBHelper.TABLE_NAME, values, selection,
					selectionArgs);
			getContext().getContentResolver().notifyChange(uri, null);
			break;

		case REDEEM_ROW_KEY:
			row_update = db.update(RedeemDBHelper.TABLE_NAME, values,
					RedeemDBHelper.REDEEM_ID + "=?",
					new String[] { uri.getLastPathSegment() });
			getContext().getContentResolver().notifyChange(uri, null);
			break;
		case REDEEM_KEY:
			row_update = db.update(RedeemDBHelper.TABLE_NAME, values,
					selection, selectionArgs);
			getContext().getContentResolver().notifyChange(uri, null);
			break;
		case QUIZ_ROW_KEY:
			row_update = db.update(QuizDBHelper.TABLE_NAME, values,
					QuizDBHelper.QUIZ_ID + "=?",
					new String[] { uri.getLastPathSegment() });
			getContext().getContentResolver().notifyChange(uri, null);
			break;
		case QUIZ_KEY:
			row_update = db.update(QuizDBHelper.TABLE_NAME, values, selection,
					selectionArgs);
			getContext().getContentResolver().notifyChange(uri, null);
			break;
		case PRICEBOOK_ROW_KEY:
			row_update = db.update(PriceBookDBHelper.TABLE_NAME, values,
					PriceBookDBHelper.PB_ID + "=?",
					new String[] { uri.getLastPathSegment() });
			getContext().getContentResolver().notifyChange(uri, null);
			break;
		case PRICEBOOK_KEY:
			row_update = db.update(PriceBookDBHelper.TABLE_NAME, values,
					selection, selectionArgs);
			getContext().getContentResolver().notifyChange(uri, null);
			break;

		case REWARD_ROW_KEY:
			row_update = db.update(RewardDBHelper.TABLE_NAME, values,
					RewardDBHelper.RW_ID + "=?",
					new String[] { uri.getLastPathSegment() });
			getContext().getContentResolver().notifyChange(uri, null);
			break;
		case REWARD_KEY:
			row_update = db.update(RewardDBHelper.TABLE_NAME, values,
					selection, selectionArgs);
			getContext().getContentResolver().notifyChange(uri, null);
			break;
		case ANALYTICDATA_ROW_KEY:
			row_update = db.update(AnalyticDBHelper.TABLE_NAME, values,
					AnalyticDBHelper.ID + "=?",
					new String[] { uri.getLastPathSegment() });
			getContext().getContentResolver().notifyChange(uri, null);
			break;
		case ANALYTICDATA_KEY:
			row_update = db.update(AnalyticDBHelper.TABLE_NAME, values,
					selection, selectionArgs);
			getContext().getContentResolver().notifyChange(uri, null);
			break;
		case PROJECT_ROW_KEY:
			row_update = db.update(ProjectDBHelper.TABLE_NAME, values,
					ProjectDBHelper.ID + "=?",
					new String[] { uri.getLastPathSegment() });
			getContext().getContentResolver().notifyChange(uri, null);
			break;
		case PROJECT_KEY:
			row_update = db.update(ProjectDBHelper.TABLE_NAME, values,
					selection, selectionArgs);
			getContext().getContentResolver().notifyChange(uri, null);
			break;
		case PROMOTION_ROW_KEY:
			row_update = db.update(PromotionDBHelper.TABLE_NAME, values,
					PromotionDBHelper.PM_ID + "=?",
					new String[] { uri.getLastPathSegment() });
			getContext().getContentResolver().notifyChange(uri, null);
			break;
		case PROMOTION_KEY:
			row_update = db.update(PromotionDBHelper.TABLE_NAME, values,
					selection, selectionArgs);
			getContext().getContentResolver().notifyChange(uri, null);
			break;

		case PRODUCT_IMAGE_ROW_KEY:
			row_update = db.update(ProductImageDBHelper.TABLE_NAME, values,
					ProductImageDBHelper.PDIM_ID + "=?",
					new String[] { uri.getLastPathSegment() });
			getContext().getContentResolver().notifyChange(uri, null);
			break;
		case PRODUCT_IMAGE_KEY:
			row_update = db.update(ProductImageDBHelper.TABLE_NAME, values,
					selection, selectionArgs);
			getContext().getContentResolver().notifyChange(uri, null);
			break;

		case USER_SETTING_ROW_KEY:
			row_update = db.update(UserSettingDBHelper.TABLE_NAME, values,
					UserSettingDBHelper.USER_ID + "=?",
					new String[] { uri.getLastPathSegment() });
			getContext().getContentResolver().notifyChange(uri, null);
			break;
		case USER_SETTING_KEY:
			row_update = db.update(UserSettingDBHelper.TABLE_NAME, values,
					selection, selectionArgs);
			getContext().getContentResolver().notifyChange(uri, null);
			break;

		case CLAIM_ROW_KEY:
			row_update = db.update(ClaimDBHelper.TABLE_NAME, values,
					ClaimDBHelper.ID + "=?",
					new String[] { uri.getLastPathSegment() });
			getContext().getContentResolver().notifyChange(uri, null);
			break;
		case CLAIM_KEY:
			row_update = db.update(ClaimDBHelper.TABLE_NAME, values, selection,
					selectionArgs);
			getContext().getContentResolver().notifyChange(uri, null);
			break;
		case IMAGE_ROW_KEY:
			row_update = db.update(ImageDBHelper.TABLE_NAME, values,
					ImageDBHelper.ID + "=?",
					new String[] { uri.getLastPathSegment() });
			getContext().getContentResolver().notifyChange(uri, null);
			break;
		case IMAGE_KEY:
			row_update = db.update(ImageDBHelper.TABLE_NAME, values, selection,
					selectionArgs);
			getContext().getContentResolver().notifyChange(uri, null);
			break;
		case REWARD_PROJECT_KEY:
			row_update = db.update(RewardProjectDBHelper.TABLE_NAME, values,
					selection, selectionArgs);
			getContext().getContentResolver().notifyChange(uri, null);
			break;
		case REWARD_PROJECT_DETAIL_KEY:
			row_update = db.update(RewardProjectDetailDBHelper.TABLE_NAME,
					values, selection, selectionArgs);
			getContext().getContentResolver().notifyChange(uri, null);
			break;
		case REWARD_CLAIM_KEY:
			row_update = db.update(RewardClaimDBHelper.TABLE_NAME, values,
					selection, selectionArgs);
			getContext().getContentResolver().notifyChange(uri, null);
			break;
		case REWARD_CLAIM_DETAIL_KEY:
			row_update = db.update(RewardClaimDetailDBHelper.TABLE_NAME,
					values, selection, selectionArgs);
			getContext().getContentResolver().notifyChange(uri, null);
			break;
		default:
			throw new IllegalArgumentException("Unknown URI: " + uri);
		}
		return row_update;
	}

}
